package fr.SDroid;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.os.Environment;
import android.util.Xml;
import android.widget.Toast;

import fr.SDroid.Graph.Graph;
import fr.SDroid.Graph.Station;


public class Write {
	
	public void writeMap(Context context, Graph g) throws Exception {
		File f = Environment.getExternalStorageDirectory();
		if (f.canWrite()) {
	        f = new File(File.separator + "sdcard" + File.separator + "TranSpeed");
	        if (!f.exists()) {
	            f.mkdir();
	            Toast.makeText(context, "cr�ation du r�pertoire", 5000).show();
	        }
	        
	        f = new File(File.separator + "sdcard" + File.separator + "TranSpeed" + File.separator, g.getVille()+"-Plan.xml");
	        
	        if(f.createNewFile())
	        	Toast.makeText(context, "fichier cr�er", 5000).show();
	        
	         
			 FileOutputStream fOut = null;
	        
	            
	       try {
	        fOut = new FileOutputStream(f, false);
	        
	        XmlSerializer serializer = Xml.newSerializer();
	        serializer.setOutput(fOut, "UTF-8");	      
	        serializer.startDocument("UTF-8", true);
	        	serializer.startTag(null, "graph");
	        	serializer.attribute(null, "ville", g.getVille());
	        	serializer.attribute(null, "version", g.getVersion().toString());
	        	for(int i = 0; i < g.getLignes().size(); i++) {
	        		serializer.startTag(null, "ligne");
	        		serializer.attribute(null, "name", g.getLignes().get(i).getName());
	        		for(int sta = 0; sta < g.getLignes().get(i).getListStation().size(); sta++) {
	        			Station station = g.getLignes().get(i).getListStation().get(sta);
	        			serializer.startTag(null, "station");
	        			serializer.attribute(null, "name", station.getName());
	        			serializer.attribute(null, "id", station.getId().toString());
	        			serializer.attribute(null, "latitude", station.getLatitude().toString());
	        			serializer.attribute(null, "longitude", station.getLongitude().toString());
	        			serializer.endTag(null, "station");
	        		}
	        		
	        		for(int sta = 0; sta < g.getLignes().get(i).getListStation().size(); sta++) {
	        			Station station = g.getLignes().get(i).getListStation().get(sta);
	        			for(int li = 0; li < station.getFils().size(); li++) {
		        			serializer.startTag(null, "lien");
		        			serializer.attribute(null, "station1", station.getId().toString());
		        			serializer.attribute(null, "station2", station.getFils().get(li).getId().toString());
		        			serializer.endTag(null, "lien");
	        			}
	        		}
	        		serializer.endTag(null, "ligne");
	        	}
	        	
	        	for(int i = 0; i < g.getLignes().size(); i++) {
	        		for(int sta = 0; sta < g.getLignes().get(i).getListStation().size(); sta++) {
	        			Station station = g.getLignes().get(i).getListStation().get(sta);
	        			for(int co = 0; co<station.getCoresp().size(); co++) {
	        				serializer.startTag(null, "coresp");
		        			serializer.attribute(null, "station1", station.getId().toString());
		        			serializer.attribute(null, "station2", station.getCoresp().get(co).getId().toString());
		        			serializer.endTag(null, "coresp");
	        			}
	        		}
	        	}
	        	
	        	serializer.endTag(null, "graph");
	        serializer.endDocument();
	        serializer.flush();
	        fOut.close();
           Toast.makeText(context, "fichier cr�er", 5000).show();
           
	       }
	       catch (IOException e) {
	    	   Toast.makeText(context, "erreur lors de la creation", 5000).show();
			e.printStackTrace();
		} 
           
	    } else {
	    	Toast.makeText(context, "peut pas ecrire", 5000);
	    }
	}

}
